#include "StartSettingsDialog.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "Network.h"

StartSettingsDialog::StartSettingsDialog()
{
    setWindowTitle(tr("Nouvelle partie"));

    m_accept = new QPushButton(tr("Accepter"), this);
    connect (m_accept, SIGNAL(clicked()), this, SLOT(accept()));

    QLabel *l_nbrPlayer = new QLabel(tr("Nombre de joueurs :"), this);
    m_nbrPlayer = new QSpinBox(this);
    m_nbrPlayer->setMinimum(2);
    m_nbrPlayer->setMaximum(6);

    QVBoxLayout *layout = new QVBoxLayout(this);
    m_rbLocal = new QRadioButton(tr("Partie hors-ligne"), this);
    connect(m_rbLocal, SIGNAL(clicked()), this, SLOT(modeChanged()));
    m_rbLocal->setChecked(true);
    layout->addWidget(m_rbLocal);
    {
        m_local = new QGroupBox(this);
        QHBoxLayout *l = new QHBoxLayout();
        l->addWidget(l_nbrPlayer);
        l->addWidget(m_nbrPlayer);
        m_local->setLayout(l);
        layout->addWidget(m_local);
    }
    m_rbOnline = new QRadioButton(tr("Partie en ligne"), this);
    connect(m_rbOnline, SIGNAL(clicked()), this, SLOT(modeChanged()));
    layout->addWidget(m_rbOnline);
    {
        m_online = new QGroupBox(this);
        QHBoxLayout *l = new QHBoxLayout();
        m_serverAddress = new QLineEdit(this);
        m_serverAddress->setText(tr("127.0.0.1"));
        l->addWidget(m_serverAddress);
        m_serverPort = new QSpinBox(this);
        m_serverPort->setMinimum(1024);
        m_serverPort->setMaximum(65534);
        m_serverPort->setValue(DEFAULT_PORT);
        l->addWidget(m_serverPort);
        m_online->setLayout(l);
        layout->addWidget(m_online);
    }
    layout->addWidget(m_accept);
    setLayout(layout);

    modeChanged();
}

void StartSettingsDialog::modeChanged()
{
    if(m_rbLocal->isChecked())
    {
        m_local->setEnabled(true);
        m_online->setEnabled(false);
    }
    else
    {
        m_local->setEnabled(false);
        m_online->setEnabled(true);
    }
}

void StartSettingsDialog::accept ()
{
    MainWindow *window;
    if(m_rbLocal->isChecked())
        window = new MainWindow(m_nbrPlayer->value());
    else
        window = new MainWindow(m_nbrPlayer->value(), m_serverAddress->text(), m_serverPort->value());

    window->show();
    close();
}
